/*
 * socket.cpp
 *
 *  Created on: 22/05/2011
 *      Author: wadi
 */

#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/types.h>

#include "socket.h"

Socket::Socket() {
    closed=false;
}

Socket::Socket(struct sockaddr_in addres, int id) {
    this->socketId = id;
    this->socketAddress = addres;
    closed=false;
}

Socket::~Socket() {

}

bool Socket::bindTo(int puerto) {
    struct sockaddr_in sa;
    memset((char*) &sa, 0, sizeof (sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons(puerto);
    sa.sin_addr.s_addr = INADDR_ANY;

    int r = bind(socketId, (struct sockaddr*) &sa, (socklen_t)sizeof (struct sockaddr));
    return r >= 0;
}

void Socket::closeSocket() {
    closed=true;
    shutdown(socketId, 2);
   close(socketId);
}


bool Socket::isAvaliable(){
    return !closed;
}
